home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / e_mac / mac_os.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-15  |  4.7 KB  |  220 lines  |  [TEXT/CWIE]

  1. #include <Files.h>
  2. #include <Types.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include "ui.h"
  6.  
  7. #if defined(__MWERKS__) || defined(THINK_C)
  8. #include <stdio.h>
  9. #include "os_mac_str.h"
  10. #include <LowMem.h>
  11. #ifndef ENOENT
  12. #define ENOENT ERANGE
  13. #endif
  14. #endif
  15.  
  16. #if ui_eventcheck_profiling_P
  17.  
  18. /* 17Jan95 e  - to find good value for PERIODIC_ACTION_FREQ */
  19.  
  20. /* see interp.c for use of ui_periodic_action */
  21.  
  22. int ui_period_counts =        4096;
  23.  
  24. #define UI_PERIOD_COUNT_UNIT   128
  25. #define MIN_UI_PERIOD_COUNT    256
  26. #define MAX_UI_PERIOD_COUNT 262144
  27.  
  28. void ui_periodic_action(void)
  29. { /* looks like MAYBE_EVENTCHK() but tunes ui_period_counts */
  30.   long ticks = LMGetTicks();
  31.   if ( ticks >= next_eventchk_ticks )
  32.   { if ( ticks > (next_eventchk_ticks + 2) && ui_period_counts > MIN_UI_PERIOD_COUNT )
  33.       ui_period_counts -= UI_PERIOD_COUNT_UNIT;
  34.     os_event_check();
  35.   }
  36.   else if ( ui_period_counts < MAX_UI_PERIOD_COUNT )
  37.     ui_period_counts += UI_PERIOD_COUNT_UNIT;
  38. }
  39.  
  40. #endif
  41.  
  42. #if defined( __MWERKS__ )
  43.  
  44. #undef chdir
  45.  
  46. int e_chdir(char *dirname)
  47. {
  48.   unsigned char buf [256];
  49.   short vrefnum;
  50.   long dirid;
  51.   HGetVol( NULL, &vrefnum, &dirid );
  52.   c_to_p (dirname, buf);
  53.   // MSL broken, was: chdir(dirname);                 // for MWERKS & std file dialog
  54.   return HSetVol( buf, vrefnum, dirid ); // for mosml
  55. }
  56.  
  57. #else
  58.  
  59. /* from Caml Light Mac 0.6 */
  60.  
  61. static short prev_wd = 0;
  62.  
  63. int chdir(char * dirname)
  64. {
  65.   unsigned char buf [256];
  66.   WDPBRec paramblock;
  67.  
  68.   if (prev_wd != 0){
  69.     paramblock.ioCompletion = NULL;
  70.     paramblock.ioVRefNum = prev_wd;
  71.     PBCloseWD (¶mblock, 0);
  72.   }
  73.  
  74. #if defined( THINK_C ) ||  defined( __MWERKS__ )
  75.   if ( strlen(dirname) > 255 ) { errno = EDOM; return -1; }
  76. #ifdef __MWERKS__
  77.   c_to_p (dirname, buf);
  78. #else
  79.   __c2p (dirname, buf);
  80. #endif
  81. #else
  82.   strncpy (buf, dirname, 255);
  83.   buf [255] = 0;
  84.   c2pstr (buf);
  85. #endif
  86.   paramblock.ioCompletion = NULL;
  87. #if defined( THINK_C ) ||  defined( __MWERKS__ )
  88.   paramblock.ioNamePtr = (unsigned char *)buf;
  89. #else
  90.   paramblock.ioNamePtr = buf;
  91. #endif
  92.   paramblock.ioVRefNum = 0;
  93.   paramblock.ioWDProcID = 'Caml';
  94.   paramblock.ioWDDirID = 0;
  95.   if (PBOpenWD (¶mblock, 0) != noErr){
  96.     errno = ENOENT;
  97.     return -1;
  98.   }
  99.   prev_wd = paramblock.ioVRefNum;
  100.  
  101.   paramblock.ioCompletion = NULL;
  102.   paramblock.ioNamePtr = NULL;
  103.   paramblock.ioVRefNum = prev_wd;
  104.   paramblock.ioWDDirID = 0;
  105.   if (PBSetVol ((ParmBlkPtr) ¶mblock, 0) != noErr){
  106.     errno = ENOENT;
  107.     return -1;
  108.   }
  109.   return 0;
  110. }
  111.  
  112. #endif
  113.  
  114. char * searchpath(char * name)
  115. {
  116.   return name;
  117. }
  118.  
  119. void set_file_type (char * name, long type)
  120. {
  121.   Str255 pname;
  122.   FInfo info;
  123.  
  124.   c_to_p( name, pname );
  125.   GetFInfo (pname, 0, &info);
  126.   info.fdType = type;
  127.   SetFInfo (pname, 0, &info);
  128. }
  129.  
  130. #if defined(THINK_C) || defined(__MWERKS__)
  131.  
  132. /* 04Jan95 e */
  133. static char *
  134. strcatbut( char *s1, char *s2 )
  135. { char c, *last_dot = 0;
  136.   while ( *s1 != 0 ) s1++;
  137.   do
  138.     switch ( c = *s2++ )
  139.     { case '.':
  140.         if ( *s2 == '.' )
  141.         { s2++; /* skip '..' */
  142.           if ( *s2 )
  143.             continue; /*      '..' becomes '' */
  144.           else
  145.             c = ':'; /* final '..' becomes ':' */
  146.         }
  147.         else if ( *s2 == '/' ) { s2++; continue; }    /* skip './' */
  148.         else if ( *s2 ==  0  ) return last_dot; /* drop final '.'  */
  149.         else last_dot = s2 - 1;
  150.         break;
  151.       case '/':
  152.         c = ':'; /* '/' becomes ':' */
  153.         break;
  154.     }
  155.   while ( *s1++ = c );
  156.   return last_dot;
  157. }
  158.  
  159. #ifdef THINK_C
  160.  
  161. char *
  162. unix_to_mac_filename (char *name, char *buffer, int buffer_len)
  163. {
  164.   int name_len = strlen(name);
  165.   /* unix name to mac name -- 11Aug93  e  */
  166.   if ( ( 1 + name_len ) > buffer_len )
  167.   {   fprintf(stderr, "\nexpand_file_name: supplied buffer is too small\n");
  168.       return(NULL);
  169.   }
  170.   if ( strchr( name, ':' ) )
  171.     strcpy( buffer, name );
  172.   else
  173.   { if ( *name == '/' ) { *buffer = 0; name++; }
  174.     else strcpy( buffer, ":" );
  175.     strcatbut( buffer, name );
  176.   }
  177.   return(buffer);
  178. }
  179.  
  180. #else
  181.  
  182. // 14Sep95 -- didn't work well with Metrowerks' open() & SFdialog
  183.  
  184. static void setMWcurdir( void )
  185. {
  186.   long cur_dir;
  187.   short cur_vol;
  188.   HGetVol( NULL, &cur_vol, &cur_dir );
  189.   LMSetCurDirStore(  cur_dir );
  190.   LMSetSFSaveDisk( - cur_vol );
  191. }
  192.  
  193. char *
  194. unix_to_mac_filename (char *name, char *buffer, int buffer_len)
  195. {
  196.   char *posn;
  197.   int name_len;
  198.   /* unix name to mac name -- 11Aug93  e  */
  199.   if ( (posn = strchr( name, '/' )) != NULL )
  200.   { name_len = strlen(name);
  201.     if ( ( 1 + name_len ) > buffer_len )
  202.     {   fprintf(stderr, "\nexpand_file_name: supplied buffer is too small\n");
  203.         return(NULL);
  204.     }
  205.     if ( posn == name ) { *buffer = 0; name++; }
  206.     else { strcpy( buffer, ":" ); setMWcurdir(); }
  207.     strcatbut( buffer, name );
  208.     return buffer;
  209.   }
  210.   else
  211.   { 
  212.     if ( *name == ':' ) setMWcurdir();
  213.     return name;
  214.   }
  215. }
  216.  
  217. #endif
  218.  
  219. #endif
  220.